package cn.edu.nks.dormitory.student.register.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import cn.edu.nks.dormitory.model.UserInfo;
import cn.edu.nks.dormitory.student.register.db.IDBService;
import cn.edu.nks.dormitory.student.register.dbimpl.DBService;
/**
 * 用户注册时验重
 * @author wangbs
 *
 */
public class ValidateRepeatAction extends HttpServlet{

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(ValidateRepeatAction.class);
	private IDBService idbService = new DBService();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("application/jsons;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		String userName = req.getParameter("userName");
		
		UserInfo user = idbService.validateRepeat(userName);
		JSONObject obj = new JSONObject();
		if(user != null){
			logger.warn(String.format("this user name %s is exist", userName));
			obj.put("status", "fail");
		}else{
			obj.put("status", "succ");
		}
		PrintWriter out = resp.getWriter();
		out.write(obj.toString());
	}
}
